<?php


namespace app\impl;


use app\common\lib\HisData;
use app\common\lib\ZhyxBeanstalkd;
use app\common\lib\ZhyxMongo;
use app\interfaces\Task;

class SyncInHospital implements Task
{

    public function run(array $data,string $taskId): ?bool
    {
        $startDate = $data["startDate"] ?? date("Y-m-d", strtotime("-1 day"));
        $endDate = $data["endDate"] ?? date("Y-m-d", strtotime("-1 day"));
        $startDate = $endDate = "2021-08-01";
        try {
            //$data = HisData::getHisData($data["type"], $startDate, $endDate);
            $data = json_decode(file_get_contents("./zhuyuan.txt"), true);
            foreach ($data as $k => $v) {
                $v["type"] = !empty($v["outhospitalDate"]) ? 3 : 2;
                $v["deleted_at"] = 0;
                $v["created_at"] = time();
                $v["patientId"] = $v["inhospitalId"];
                //判断患者是否存在
                $result = ZhyxMongo::getInstance()->setCollection("patient_record")->updateOne(["no" => $v['inhospitalNo']], ['$set' => $v], ['upsert' => true]);
                if ($result->getUpsertedId()) {
                    //像患者表中填入记录
                    ZhyxMongo::getInstance()->setCollection("patient_info")->updateOne(["patientId" => $v["patientId"]], ['$push' => [
                        'timeLine' => [
                            'type' => !empty($v["outhospitalDate"]) ? 3 : 2,
                            'no' => $v['inhospitalNo'],
                            'sex' => $v['sex'],
                            'age' => $v["Inage"],
                            'date' => $v['inhospitalDate'],
                            'dept_id' => $v['inhospitalDeptCode'],
                            'deptName' => $v['inhospitalDept'],
                            'ward' => $v['inhospitalWard'],
                            'leave_date' => $v['outhospitalDate'],
                            'out_dept_id' => $v["outhospitalDeptCode"],
                            'out_dept_name' => $v["outhospitalDept"],
                            'out_ward' => $v["outhospitalWard"],
                            'way' => $v["inhospitalWay"] ?: 9,
                            'doctorId' => $v['inchargeDoctorNo'] ?? "",
                            'doctorName' => $v['inchargeDoctorName'] ?? "",
                            'medicalRecordsNo' => $v['medicalRecordsNo'],
                            'diagnosis' => $v['diagnose'] ?: ""
                        ]
                    ]]);

                    //像患者表中填入记录
                    if ($v["type"] === 2) {
                        $param = ["inList" => $v];
                    } else {
                        $param = ["hoList" => $v];
                    }
                    ZhyxMongo::getInstance()->setCollection("patient_info")->updateOne(["patientId" => $v["patientId"]], ['$set' => $param]);
                }
            }
        } catch (\Exception $exception) {
            echo $exception->getMessage();
            exit();
            \think\facade\Log::record("同步患者信息异常:" . $exception->getMessage(), "error");
        }

        return true;
    }
}